#include "bencode.h"

bString::bString(const char *bString) : bItem(B_STRING)
{
	const char *ptr = bString;
	m_strlen = 0;
	while (ptr[0] != ':')
	{
		m_strlen = 10*m_strlen + (int)(ptr++[0]-'0');
	}
	m_str = (char*)malloc(sizeof(char)*(m_strlen+1));
	memcpy(m_str, ++ptr, m_strlen);
	m_str[m_strlen] = '\0';
	m_charoffset = ptr+m_strlen - bString;
}

bString::~bString()
{
	free(m_str);
}

string bString::GetString()
{
	return string(m_str);
}

void bString::GetByteString(char *dest)
{
	memcpy(dest, m_str, m_strlen);
}

unsigned bString::GetStringLength()
{
	return m_strlen;
}
